﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.TownHall.ServiceClient;
using NASA.BeAMartian.Model;

namespace NASA.BeAMartian.Views.Learn
{
    public partial class QuestionListPage : PhoneApplicationPage
    {
        object _selectedItem;
        public THServiceClient client = new THServiceClient();
        public NuggetList Nuggets = new NuggetList();

        //textBlockPageTitle
        public QuestionListPage()
        {
            InitializeComponent();

            client.QuestionsDownloaded += new QuestionsDownloadedEventHandler(client_QuestionsDownloaded);

            FrameworkElement _root = Application.Current.RootVisual as FrameworkElement;
            ClientState clientState = (ClientState)_root.DataContext;
            if (clientState == null) { clientState = new ClientState(); }

            Topic t = clientState.CurrentTopic;
            textBlockPageTitle.Text = String.Format("{0} - QUESTIONS", clientState.CurrentTopic.Name.ToUpper());            

            string categorySlug = "test";
            if (t != null)
            {
                if (t.Slug != null)
                {
                    categorySlug = t.Slug;
                }
            }
            client.GetQuestionsForTopic(categorySlug, 1, 10);
        }

        void client_QuestionsDownloaded(object sender, QuestionsDownloadedEventArgs e)
        {
            Nuggets = e.Nuggets;

            Dispatcher.BeginInvoke(delegate()
            {
                FrameworkElement _root = Application.Current.RootVisual as FrameworkElement;
                ClientState clientState = (ClientState)_root.DataContext;
                if (clientState == null) { clientState = new ClientState(); }

                clientState.CurrentTopicQuestions = (NuggetList)e.Nuggets;
                _root.DataContext = clientState;
                lbQuestions.ItemsSource = Nuggets;
            });
        }

        private void btnAskAQuestion_Click(object sender, RoutedEventArgs e)
        {
            if (ApplicationContext.Instance.UserData == null)
                NavigationService.Navigate(new Uri("/Views/SignIn.xaml?return=question", UriKind.Relative));
            else
                this.NavigationService.Navigate(new Uri("/Views/Learn/AskQuestionPage.xaml", UriKind.Relative));
        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FrameworkElement _root = Application.Current.RootVisual as FrameworkElement;
            ClientState clientState = (ClientState)_root.DataContext;
            if (clientState == null) { clientState = new ClientState(); }
            clientState.CurrentQuestion = lbQuestions.SelectedItem as Nugget;
            _root.DataContext = clientState;

            this.NavigationService.Navigate(new Uri("/Views/Learn/QuestionPage.xaml", UriKind.Relative));
        }
    }
}